<?php
/**
 * 后台公共控制器，登录退出
 * Created by PhpStorm.
 * User: yhz
 * Date: 2017-12-19
 * Time: 17:34
 */
namespace app\admin\controller;

use app\admin\auth\Auth;
use app\common\model\AdminUsers;
use think\Controller;
class Pub extends Controller
{
    protected $param;
    public function __construct()
    {
        parent::__construct();
        $this->param = $this->request->param();
    }
    public function login()
    {
        if ($this->request->isPost()) {
            //验证参数
            $validate = [
                ['username|账号', 'require'],
                ['password|密码', 'require']
            ];
            $result = $this->validate($this->param, $validate);
            if ($result!==true) {
                return $this->loginError($result); //验证不通过处理
            }
            //获取账号密码参数并封装数组
            $data = [
                'user_name' => $this->param['username'],
                'password' => md5($this->param['password']),
            ];
            $admin_users = new AdminUsers();
            $admin_user  =$admin_users->get($data);
            if ($admin_user) {
                //判断账户是否被冻结
                if ($admin_user->getData('status') != 1) {
                    return $this->loginError('账户被冻结');
                }


                if (isset($this->param['remember'])) {
                    //执行登陆，记住账号
                    Auth::login($admin_user['user_id'], $admin_user['user_name'], true);
                } else {
                    //执行登陆,不记账号
                    Auth::login($admin_user['user_id'], $admin_user['user_name'], false);
                }
                //写入日志
                $redirect_uri = isset($this->param['uri']) ? $this->param['uri'] : 'admin/index/index';
                return $this->loginSuccess('登陆成功',$redirect_uri);
            }
            return $this->loginError('账号或密码错误'); //验证不通过处理
        }
        return $this->fetch();
    }

    //退出
    public function logout()
    {
        $auth = new Auth();
        $auth->logout();
        $this->redirect('pub/login');
    }



    /**
     * 登录成功
     * @param string $msg
     * @param null $url
     * @param string $data
     */
    protected function loginSuccess($msg = '', $url = null, $data = '')
    {
        if ($url == null) {
            $url = url(  'admin/index/index');
        }
        $return_data =[
            'success_message' => $msg,
            'url'=>$url,
            'data'=>$data
        ];
//         $this->redirect($url, $data, 302, ['success_message' => $msg]);
        return json($return_data);

    }

    /**
     * 登录错误
     * @param string $msg
     * @param null $url
     * @param string $data
     */
    protected function loginError($msg = '', $url = null, $data = '')
    {
        if ($url == null) {
            $url = $this->request->server('HTTP_REFERER');
        }

        if ($msg == '') {
            $msg = '操作失败！';
        }
        $return_data =[
            'error_message' => $msg,
            'url'=>$url,
            'data'=>$data
        ];
        return json($return_data);
    }
}